﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Controls;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;

namespace SharpDiary.Note
{
    public partial class NoteInfoListItemControl : ListItemControlBase
    {
        private NoteInfo _note;

        public NoteInfo Note
        {
            get { return _note; }
            set
            {
                _note = value;
                DisplayItemInfo();
            }
        }

        private int _summaryTextMaxLength = 20;

        /// <summary>
        /// 摘要文字的最大长度。
        /// </summary>
        public int SummaryTextMaxLength
        {
            get { return _summaryTextMaxLength; }
            set { _summaryTextMaxLength = value; }
        }

        public NoteInfoListItemControl()
        {
            InitializeComponent();
        }


        protected override void DisplayItemInfo()
        {
            base.DisplayItemInfo();

            if (_note == null) { return; }
            else
            {
                Title = _note.Title;
                Description = Utils.GetSummaryText(_note.Content, _summaryTextMaxLength);
                CreatedAndUpdateDatetime = _note.CreatedDatetime.ToString() + " 创建";
            }
        }

        protected override void OnEditButtonClick(object sender, EventArgs e)
        {
            base.OnEditButtonClick(sender, e);

            UserSettings.ShellInstance.ExecuteAction("SharpDiary.Note", "c1498ab4-7301-4c30-8de1-e9516d6e9724", _note, this);
        }

        /// <summary>
        /// 删除笔记。
        /// </summary>
        private void DeleteNote()
        {
            if (_note == null) { return; }

            if (MessageBoxHelper.ShowConfirmation("您确定要删除此笔记吗？", true) == DialogResult.Yes)
            {
                if (NoteInfo.Remove<NoteInfo>(_note))
                {
                    this.Hide();
                    this.Dispose();
                }
                else
                {
                    MessageBoxHelper.ShowAlert("删除笔记失败。");
                }
            }
        }

        protected override void OnDeleteButtonClick(object sender, EventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);

            DeleteNote();
        }
    }
}
